<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 领奖纪录操作功能
 * @package  htdocs
 * @subpackage  quanliadmin
 * @create by allen.qiang
 */
quanli_require('module/base/promotionHandler.class.php');
class pa_awardRecord {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']){
                case 1: //领奖纪录列表
                    $this->awardRecordList();
                    break;
                case 2://导出
                    $this->awardRecordExport();
                    break;
                case 3://修改
                    $this->awardRecordEdit();
                    break;
                default:
                    $this->toError('参数错误');
                    break;
            }

        } else {

            $this->toError('参数错误');

        }

    }

    /**
     * 转跳到错误页面
     * @param string $msg 错误信息
     */
    private function toError( $msg = '' ) {

        global $G_PGLOB;

        $G_PGLOB->display_error_page( $msg );

    }

    /**
     * 领奖人纪录列表
     */
    private function awardRecordList() {

        global $G_PGLOB;

        $edit_permission = $G_PGLOB->has_permission(4523);

        $G_PGLOB->smarty->assign('edit_permission',$edit_permission);

        $G_PGLOB->smarty->display('promotion/awardRecord.tpl');

    }

    //领奖纪录导出
    private function awardRecordExport(){

        global $G_PGLOB;

        //检查phpexcel类
        quanli_include('framework/phpExecl/PHPExcel.php');
        quanli_include('framework/phpExecl/PHPExcel/Writer/Excel2007.php');

        $arg_array = $_REQUEST;

        $arg_array['sortname'] = 'createTime';
        $arg_array['sortorder'] = 'desc';

        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        //查询中奖纪录
        $result_data = $promotionHandler->awardRecordList($arg_array);


        //实例excel类
        $objPHPExcel = new PHPExcel();

        /**
         * 设置excel的属性
         */
        //创建人
        $objPHPExcel->getProperties()->setCreator("$G_PGLOB->currentUserName");
        //最后修改人
        $objPHPExcel->getProperties()->setLastModifiedBy("$G_PGLOB->currentUserName");
        //标题
        $objPHPExcel->getProperties()->setTitle("领奖纪录明细列表");
        //题目
        $objPHPExcel->getProperties()->setSubject("领奖纪录明细列表");
        //描述
        $objPHPExcel->getProperties()->setDescription("领奖纪录明细列表");
        /**
         * 设置excel的属性
         */

        /**
         * sheet
         */
        //设置当前的sheet
        $objPHPExcel->setActiveSheetIndex(0);
        //设置sheet的name
        $objPHPExcel->getActiveSheet()->setTitle('领奖纪录明细列表');
        /**
         * sheet
         */


        /**
         * 设置标题
         */
        $objPHPExcel->getActiveSheet()->setCellValue('A1', '领奖人');
        $objPHPExcel->getActiveSheet()->setCellValue('B1', '领奖人手机号码');
        $objPHPExcel->getActiveSheet()->setCellValue('C1', '收货地址');
        $objPHPExcel->getActiveSheet()->setCellValue('D1', '身份证号码');
        $objPHPExcel->getActiveSheet()->setCellValue('E1', '领取的礼品');
        $objPHPExcel->getActiveSheet()->setCellValue('F1', '领奖日期');
        /**
         * 设置标题
         */

        /**
         * 添加数据
         */
        if ($result_data) {

            $i = 2;

            foreach($result_data as &$val) {

                $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $val['custName'] . "\t");
                $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $val['mobile']);
                $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $val['address']);
                $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $val['identityCard']."\t");
                $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $val['prize']);
                $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $val['createTime']);
                $i++;

            }

        }
        /**
         * 添加数据
         */

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(35);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(25);

        //文件名称
        $fileName = '领奖纪录_' . date('YmdHis') . '.xlsx';

        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        header("Content-Type: application/force-download");
        header("Content-Disposition:inline;filename=$fileName");
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');

    }

    //领奖纪录编辑
    private function awardRecordEdit(){

        global $G_PGLOB;

        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        $receive_prize = $promotionHandler->awardRecordList($arg_array);

        $G_PGLOB->smarty->assign_by_ref('receive_prize',$receive_prize);

        $G_PGLOB->smarty->display('promotion/awardEdit.tpl');

    }

}